﻿using Omnia.DataModels;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using Omnia.Library;
using Omnia.Library.Extensions;
using System.Windows.Input;
using System.Windows;

namespace Omnia.ViewModels
{
    public class TransactionEditViewModel : MainViewModel
    {
        #region Properties
        private string guid;
        public string Guid
        {
            get { return guid; }
            set
            {
                guid = value;
                RaisePropertyChanged(() => Guid);
                if (!IsDesignMode)
                {
                    CurrentEditTransactionItem = Database.CollectionOfTransactions.FirstOrDefault(t => t.Guid == Guid);
                    LinkToCase = !string.IsNullOrWhiteSpace(CurrentEditTransactionItem.CaseGuid);
                    RaisePropertyChanged(() => CurrentEditTransactionItem);
                    RaisePropertyChanged(() => ShowCategories);
                    SelectedCategoty = Database.CollectionOfCategories.FirstOrDefault(c => c.Guid == CurrentEditTransactionItem.CategoryGuid);
                    SelectedCase = Database.CollectionOfCases.FirstOrDefault(c => c.Guid == CurrentEditTransactionItem.CaseGuid);
                }
            }
        }

        private bool linkToCase = false;
        public bool LinkToCase
        {
            get { return linkToCase; }
            set
            {
                linkToCase = value;
                RaisePropertyChanged(() => LinkToCase);
            }
        }

        private TransactionInfo currentEditTransactionItem;
        public TransactionInfo CurrentEditTransactionItem
        {
            get { return currentEditTransactionItem; }
            set
            {
                currentEditTransactionItem = value;
                RaisePropertyChanged(() => CurrentEditTransactionItem);
                RaisePropertyChanged(() => ShowCategories);
            }
        }

        private CategoryInfo selectedCategory;
        public CategoryInfo SelectedCategoty
        {
            get { return selectedCategory; }
            set
            {
                selectedCategory = value;
                RaisePropertyChanged(() => SelectedCategoty);
                if (CurrentEditTransactionItem != null)
                {
                    CurrentEditTransactionItem.CategoryGuid = SelectedCategoty.Guid;
                    RaisePropertyChanged(() => CurrentEditTransactionItem);
                }
            }
        }

        private CaseInfo selectedCase;
        public CaseInfo SelectedCase
        {
            get { return selectedCase; }
            set
            {
                selectedCase = value;
                RaisePropertyChanged(() => SelectedCase);
            }
        }

        public ObservableCollection<CategoryInfo> ShowCategories
        {
            get
            {
                if (CurrentEditTransactionItem != null)
                {
                    return new ObservableCollection<CategoryInfo>(Database.CollectionOfCategories.Where(c => c.Kind == CurrentEditTransactionItem.Kind));
                }
                return null;
            }
        }

        #endregion

        #region Method
        public void RefreshCategory()
        {
            RaisePropertyChanged(() => ShowCategories);
        }

        private void Update(TransactionInfo entity)
        {
            if (LinkToCase && SelectedCase != null)
            {
                entity.CaseGuid = SelectedCase.Guid;
            }
            if (!string.IsNullOrWhiteSpace(entity.Place) && GlobalSettings.AutoSavePlace)
            {
                if (StorePlaceCollection.Count(p => p.Place == entity.Place) == 0)
                {
                    Database.CollectionOfPlaces.InsertOnSubmit(new PlaceInfo() { Place = entity.Place });
                }
            }
            var item = Database.CollectionOfTransactions.FirstOrDefault(t => t.Guid == entity.Guid);
            item = entity;
            Save();
        }
        #endregion

        #region Commands
        public ICommand UpdateCommand
        {
            get
            {
                return new GenericCommand()
                {
                    ExecuteCallback = delegate
                    {
                        try
                        {
                            Update(CurrentEditTransactionItem);
                            //MessageBox.Show(Application.Current.FindResource<string>(ResourceKeys.IDS_NOTE_TRANS_UPDATE_OK), Application.Current.FindResource<string>(ResourceKeys.IDS_NOTE_TRANS_UPDATE_TITLE), MessageBoxButton.OK);
                            NavigateBack();
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message, Application.Current.FindResource<string>(ResourceKeys.IDS_NOTE_TRANS_UPDATE_ERROR), MessageBoxButton.OK);
                        }
                    }
                };
            }
        }
        #endregion
    }
}
